package com.csii.epaySandbox.web;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

/**
 * HttpClineUtil.java
 * 用HTTPCLIENT方式向服务器发送请求
 * 并接收服务器返回数据初步解析数据
 */
public class HttpClientUtil {
	
	/**
	 * 
	 * 将String类型的报文发送至联调环境网关并接收返回报文
	 *
	 */
   public static String getDataFromServer(String transAbbr,String url) throws Exception{
	   String resp = null;
	   Logger.getRootLogger().info("Send to Server's Data >>>   "+transAbbr);
	   HttpClient httpClient = new HttpClient();
	   httpClient.setConnectionTimeout(10000);
	   PostMethod pm = new PostMethod(url);
	   pm.setRequestBody(transAbbr);
	   try {
		   httpClient.executeMethod(pm);
		   resp = pm.getResponseBodyAsString();
		   Logger.getRootLogger().info("Get data from server : >>>   "+resp);
	   } catch (HttpException e) {
		   Logger.getRootLogger().error(e);
		   throw new HttpException("http 请求错误");
	   } catch (IOException e) {
		   Logger.getRootLogger().error(e);
		   throw new IOException("IO错误");
	   }finally{
		   pm.releaseConnection();
	   }
	   return resp;
   }
   
   /*
    * 解析应答报文获取所需字段
    */
   public static void ResolveData(String data) throws Exception{
	   
   }
   
   public static Map<String,String>  ResolveData2(String data) {
	   String orderData = "";
	   int begin = data.indexOf("<Plain>");
	    int end = data.lastIndexOf("</Plain>");
	    if(begin != -1){
	    	orderData = data.substring(begin+7, end);
		}
	    if(orderData == ""){
			if(data.indexOf("<ErrorMsg>") != -1)
				Logger.getRootLogger().info(data.substring(data.indexOf("<ErrorMsg>")+10, data.lastIndexOf("</ErrorMsg>")));
			return null;
		}
	    Map<String,String> ret = new HashMap<String,String>();
	    String[] values = orderData.split("[|]");
	    for(String s:values){
	    	String[] s1 = s.split("=");
	    	if(s1.length<2){
	    		continue;
	    	}
	    	ret.put(s1[0], s1[1]);
	    }
	   return ret; 
   }
   
 public static Map<String,String>  ResolveData2WithErr(String data) throws Exception{
	   String orderData = "";
	   Map<String,String> ret = new HashMap<String,String>();
	   int begin = data.indexOf("<Plain>");
	   int end = data.lastIndexOf("</Plain>");
	    if(begin != -1){
	    	orderData = data.substring(begin+7, end);
		}
	    if(orderData == ""){
			if(data.indexOf("<ErrorMsg>") != -1){
				int begin1 = data.indexOf("<ErrorCode>");
				int end1 = data.lastIndexOf("</ErrorCode>");
				String errorCode = data.substring(begin1+11, end1);
				int begin2 = data.indexOf("<ErrorMsg>");
				int end2 = data.lastIndexOf("</ErrorMsg>");
				String errorMsg = data.substring(begin2+10, end2);
				ret.put("ErrorCode", errorCode);
				ret.put("ErrorMsg", errorMsg);
			}
			else	
				throw new Exception(data);
			
		}else{
			 String[] values = orderData.split("[|]");
			    for(String s:values){
			    	String[] s1 = s.split("=");
			    	if(s1.length<2){
			    		continue;
			    	}
			    	ret.put(s1[0], s1[1]);
			    }
		}
	   return ret;
   }
   
   /*
    * 获取应答报文中签名部分
    */
   public static Map<String,String> getPlainAndSignature(String data) {
	   String plain = "";
	   String signature="";
	   Map<String,String> map = new HashMap<String,String>();
	   int begin = data.indexOf("<Signature>");
	   int end = data.lastIndexOf("</Signature>");
	   int begin2 = data.indexOf("<Plain>");
	   int end2 = data.lastIndexOf("</Plain>");
	   if(begin != -1){
		   plain=data.substring(begin2+7, end2);
		   signature = data.substring(begin+11, end);
		   map.put("Plain", plain);
		   map.put("Signature", signature);
		}
	   return map;
   }
}
